var pattern = new RegExp(/\?Code=(\S*)/g);
var idx = pattern.exec(window.location.href);

var _grid = $('#jgTable');

var _Data = [];

$(function(){
    requestAjax();
    _gridInit();
    $('li[data-opt="u_save"],button[data-opt="u_save"]').on('click',function(){ //MDM0009/MDM000912
        //console.log($('#jgTable').jqGrid('getRowData'));
        var requestArr = [];
        _grid.jqGrid('getRowData').forEach(function(v,i,a){
            requestArr.push({'Code':v.Code,'OrderId':i+1})
        });
        console.log(requestArr);
        var requestData = Object.create(postData);
        requestData.Method = 'U';
        requestData.Content = requestArr;
        $.ajax({
            type: "post",
            url: mdmPath+'/MDM0009/MDM000912',
            data: requestData,
            dataType: "json",
            success: function (re) {
                if (re.Status == 200) {
                    myAlert('保存成功');
                } else {
                    myAlert(re.Message);
                }
            }
        });
    });
});

function requestAjax(){   //MDM0009/MDM000907
    var _postData = Object.create(postData);
    _postData.Method = 'Q';
    _postData.Content = {
        PropertyCode: idx[1] 
    };
    $.ajax({
        async: false,
        type: "post",
        url: mdmPath + '/MDM0009/MDM000907',
        data: _postData,
        dataType: "json",
        success: function (re) {
            if (re.Status == 200) {
                loadAjax(re);
            } else if (re.Status == -999) {
                alert_QX($('body'), re.Message);
            } else {
                alert(re.Message);
            }
        }
    });
}
function loadAjax(re){
    if(re.Content){
        _Data = re.Content.loMDM000907ReturnList;
    }
}

function _gridInit(){
    var _gridData = Object.create(gridData);
    _gridData.datatype = "local";
    _gridData.rowNum = '100000';
    _gridData.colModel = [
        {label:'物料代码', name:'Code'},
        {label:'物料名称', name:'Name'},
        {label:'核心规格', name:'Mainproperty'},
        {label:'制造商型号', name:'ManufactureNo'},
        {label:'价格', name:'price', width:60}
    ];
    _gridData.beforeRequest = function(){
        _gridData.grid.jqGrid("setGridParam", {
            data : _Data
        });
    };
    _gridData.loadComplete = function(xhr){ 
        var rowData = _gridData.grid.jqGrid("getRowData");
        $('a[opt-data="jgTable_delete"]').unbind('click').bind('click',function(){
            var rowId = $(this).parent().parent()[0].id;
            _gridData.grid.jqGrid("delRowData",rowId);
            _Data.forEach(function(v,i,a){
                v.Code===rowData[rowId-1].Code&&_Data.splice(i,1);
            });
            //console.log(_Data)
        });
        _gridData.grid.jqGrid('sortableRows',{
            update:function(ev,ui){
                var item = ui.item[0],
                ri = item.rowIndex,
                itemId = item.id;
            }
        });
    };
    gridInit(_gridData);
}

function addSubmit(){
    // console.log('提交按钮');
    mData.forEach(function(v,i,a){
        if(checked.indexOf(v.Code) != -1){
            _Data.push(v);
        }
    });
    _Data = _Data.unique('Code');
    _grid.jqGrid("clearGridData").trigger("reloadGrid");
    myModal.addClass('hidden');
}

var _gridWidth = $('.u_toolbar');
var gridWidth0m = $('.u_modal_body');
function gridSize(){ 
    _grid.setGridWidth(_gridWidth.width());
}
